package cn.itsource.pethome.system.shop.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.jwt.LoginContextUtils;
import cn.itsource.pethome.system.shop.domain.Shop;
import cn.itsource.pethome.system.shop.domain.ShopAuditLog;
import cn.itsource.pethome.system.shop.mapper.ShopMapper;
import cn.itsource.pethome.system.shop.service.IShopAuditLogService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.system.user.domain.Employee;
import cn.itsource.pethome.system.user.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2023-01-08
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LoginContextUtils loginContextUtils;

    @Override
    public void add(ShopAuditLog shopAuditLog,HttpServletRequest req) {

        Employee loginUser = (Employee)loginContextUtils.getLoginUser(req);
        //构建默认参数
        shopAuditLog.setAuditTime(new Date());
        //由于没有做登录 所以目前只能写的假的
        shopAuditLog.setAuditId(loginUser.getId());
        //得到店铺
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        //修改状态
        shop.setState(shopAuditLog.getState());
        //执行修改
        shopMapper.update(shop);
        //邮箱的地址
        Long adminId = shop.getAdminId();
        //拿到管理员的邮箱
        Employee one = employeeMapper.findOne(adminId);
        String email = one.getEmail();

        if(shopAuditLog.getState() == BaseConstants.Shop.STATE_WAIT_ACTIVE){
            //发送邮件
            toEmail("宠物之家店铺激活",shop.getId(),email);
        }else {
            toEmail("宠物之家店铺消息",null,email);
        }
        super.add(shopAuditLog);
    }

    private void toEmail(String titleMsg,Long shopId,String toEmail){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1972629647@qq.com");
            helper.setSubject(titleMsg);
            //第二个参数 true 代表是否是一个html内容
            if(shopId == null){
                helper.setText("<h1>你店铺申请被驳回</h1>",true);
            }else {
                helper.setText("<h1>"+titleMsg+"</h1><a href='http://127.0.0.1:8080/shop/activation/"+shopId+"'>点击激活账号</a>",true);
            }

            //添加附件
            //helper.addAttachment("美女图片.jpg",new File("C:\\Users\\Administrator\\Desktop\\img\\107103188_157720105843055_1832181020318906103_o.jpg"));
            //helper.addAttachment("压缩文件", new File("C:\\resources.zip"));
            //收件人
            helper.setTo(toEmail);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
